package com.example.palette.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.palette.R;
import com.example.palette.bean.FileDetected;

import java.util.List;

public class FileAdapter extends BaseAdapter<FileDetected> {
    public FileAdapter(int layoutId, List<FileDetected> datas) {
        super(layoutId, datas);
    }

    @Override
    public void bindViews(BaseHolder holder, FileDetected fileDetected, int position) {
        TextView name = holder.getView(R.id.tv_rv_file_item_fileName);
        ImageView fileType = holder.getView(R.id.iv_rv_file_item_fileType);
        name.setText(fileDetected.getName().substring(fileDetected.getName().lastIndexOf("/") + 1));
        name.setTag(R.id.file_name_id,fileDetected);
        name.setTag(R.id.file_name_position,position);
        if(fileDetected.getFileType() == FileDetected.FILE_TYPE_DIRECTORY){
            fileType.setImageResource(R.drawable.img_folder);
        }else {
            fileType.setImageResource(R.drawable.img_file);
        }
        ImageView checkBox = holder.getView(R.id.iv_rv_file_item_select);
        if(fileDetected.isSelected()){
            checkBox.setImageResource(R.drawable.img_checked);
        }else {
            checkBox.setImageResource(R.drawable.img_unchecked);
        }
    }

    @Override
    public void bindListener(BaseHolder holder) {
        TextView name = holder.getView(R.id.tv_rv_file_item_fileName);
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickListen != null){
                    onItemClickListen.onItemClick((FileDetected) name.getTag(R.id.file_name_id), (Integer) name.getTag(R.id.file_name_position),R.id.tv_rv_file_item_fileName);
                }
            }
        });
        ImageView select = holder.getView(R.id.iv_rv_file_item_select);
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickListen != null){
                    onItemClickListen.onItemClick(null,(Integer) name.getTag(R.id.file_name_position),R.id.iv_rv_file_item_select);
                }
            }
        });
    }
}
